const rules = {
  data() {
    // 库存预警值自定义验证
    const validateStockWarn = (rule, value, callback) => {
      if (value <= 0 || value > 100) {
        callback(new Error('请输入1-100之间的整数'))
      } else {
        callback()
      }
    }
    // 虚拟销量自定义验证
    const validVirtualSales = (rule, value, callback) => {
      if (value <= 0 || value > 1000) {
        callback(new Error('请输入1-1000之间的整数'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          },
          {
            max: 50,
            message: '商品名称在50字以内',
            trigger: 'blur'
          }
        ],
        images: [
          {
            type: 'array',
            required: true,
            message: '请输上传商品图片',
            trigger: 'change'
          }
        ],
        // 品牌id
        brandId: [
          {
            required: true,
            message: '请选择商品品牌',
            trigger: 'blur'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入商品排序',
            trigger: 'blur'
          },
          {
            pattern: /^[1-9]\d{0,3}$/,
            message: '请输入1-9999'
          }
        ],
        // 库存预警值
        stockWarningQuantity: [
          {
            required: true,
            message: '请输入库存预警值',
            trigger: 'blur'
          },
          { type: 'number', message: '请输入数字', trigger: ['blur', 'change'] },

          { validator: validateStockWarn, trigger: ['blur', 'change'] }
        ],
        // 虚拟销量
        virtualSalesQuantity: [
          {
            required: true,
            message: '请输入虚拟销量',
            trigger: 'blur'
          },
          { type: 'number', message: '请输入数字', trigger: ['blur', 'change'] },
          { validator: validVirtualSales, trigger: ['blur', 'change'] }
        ]
      }
    }
  }
}

export default rules
